/**    
 * @{#} DeviceUtility.java Create on 2014年7月18日 上午9:50:41    
 *    
 * @author <a href="mailto:evan0502@qq.com">Evan</a>   
 * @version 1.0    
 */
package com.evan.common.utils;

import java.net.InetAddress;
import java.net.NetworkInterface;
import java.net.SocketException;
import java.util.Enumeration;

import org.apache.http.conn.util.InetAddressUtils;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.app.Service;
import android.text.ClipboardManager;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.net.wifi.WifiInfo;
import android.net.wifi.WifiManager;
import android.os.Vibrator;

@SuppressWarnings("deprecation")
public class DeviceUtility {

	public static final class DeviceInfo {
		public static String getMac(Context context) {
			WifiManager wifi = (WifiManager) context
					.getSystemService(Context.WIFI_SERVICE);
			WifiInfo info = wifi.getConnectionInfo();
			return info.getMacAddress();
		}

		public static String getLocalHostIp() {
			try {
				Enumeration<NetworkInterface> en = NetworkInterface
						.getNetworkInterfaces();
				// 遍历所用的网络接口
				while (en.hasMoreElements()) {
					NetworkInterface nif = en.nextElement();// 得到每一个网络接口绑定的所有ip
					Enumeration<InetAddress> inet = nif.getInetAddresses();
					// 遍历每一个接口绑定的所有ip
					while (inet.hasMoreElements()) {
						InetAddress ip = inet.nextElement();
						if (!ip.isLoopbackAddress()
								&& InetAddressUtils.isIPv4Address(ip
										.getHostAddress())) {
							return ip.getHostAddress();
						}
					}
				}
			} catch (SocketException e) {
				e.printStackTrace();
			}
			return "";
		}

		public static int getSystemSDKVersion() {
			int version = 0;
			try {
				version = Integer.valueOf(android.os.Build.VERSION.SDK_INT);
			} catch (NumberFormatException e) {
			}
			return version;
		}
	}

	public static final class DeviceController {
		public static void Vibrate(final Context activity, long milliseconds) {
			Vibrator vib = (Vibrator) activity
					.getSystemService(Service.VIBRATOR_SERVICE);
			vib.vibrate(milliseconds);
		}

		public static void Vibrate(final Context activity, long[] pattern,
				boolean isRepeat) {
			Vibrator vib = (Vibrator) activity
					.getSystemService(Service.VIBRATOR_SERVICE);
			vib.vibrate(pattern, isRepeat ? 1 : -1);
		}
	}

	@SuppressLint("NewApi")
	@SuppressWarnings("deprecation")
	public static final class SystemOperate {

		/**
		 * 发送短信
		 * 
		 * @param smsBody
		 */
		public static void sendSMS(Context context, String tel, String smsBody) {
			Uri smsToUri = Uri.parse("smsto:" + tel);
			Intent intent = new Intent(Intent.ACTION_SENDTO, smsToUri);
			intent.putExtra("sms_body", smsBody);
			context.startActivity(intent);
		}

		public static void copy2Clipboard(Activity activity, String str,
				String tip) {
			ClipboardManager clipboard = (ClipboardManager) activity
					.getSystemService(Context.CLIPBOARD_SERVICE);
			clipboard.setText(str);
			CommonUtility.toast(activity,
					CommonUtility.isNull(tip) ? "内容已复制到剪贴板中。" : tip);
		}

		public static void copy2Clipboard(Activity activity, String str) {
			copy2Clipboard(activity, str, null);
		}
	}

}
